package com.electromagnetic.industry.software.manage.controller;

import com.electromagnetic.industry.software.common.annotations.RequiredRole;
import com.electromagnetic.industry.software.common.annotations.UserOperation;
import com.electromagnetic.industry.software.common.enums.AdminTypeEnum;
import com.electromagnetic.industry.software.common.enums.UserOperationModuleEnum;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import com.electromagnetic.industry.software.manage.pojo.req.RoleDTO;
import com.electromagnetic.industry.software.manage.pojo.req.RolePageDTO;
import com.electromagnetic.industry.software.manage.service.RoleService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/data/ed/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @RequiredRole(AdminTypeEnum.SECURITY)
    @UserOperation(value = "新建角色", modelName = UserOperationModuleEnum.PERMISSION)
    @RequestMapping(value = "/createRole", method = RequestMethod.POST)
    public ElectromagneticResult<?> createRole(@RequestBody RoleDTO roleDTO) {
        return ElectromagneticResultUtil.success(roleService.createRole(roleDTO));
    }

    @RequiredRole(AdminTypeEnum.SECURITY)
    @UserOperation(value = "更新角色", modelName = UserOperationModuleEnum.PERMISSION)
    @RequestMapping(value = "/updateRole", method = RequestMethod.POST)
    public ElectromagneticResult<?> updateRole(@RequestBody RoleDTO roleDTO) {
        return ElectromagneticResultUtil.success(roleService.updateRole(roleDTO));
    }

    @RequiredRole(AdminTypeEnum.SECURITY)
    @UserOperation(value = "删除角色", modelName = UserOperationModuleEnum.PERMISSION)
    @GetMapping(value = "/deleteRole/{roleId}")
    public ElectromagneticResult<?> deleteRole(@PathVariable("roleId") String roleId) {
        return ElectromagneticResultUtil.success(roleService.deleteRole(roleId));
    }

    @RequiredRole(AdminTypeEnum.SECURITY)
    @UserOperation(value = "查看指定角色", modelName = UserOperationModuleEnum.PERMISSION)
    @GetMapping(value = "/getSingleRole/{roleId}")
    public ElectromagneticResult<?> getRole(@PathVariable("roleId") String roleId) {
        return ElectromagneticResultUtil.success(roleService.getRole(roleId));
    }

    @RequiredRole(AdminTypeEnum.SECURITY)
    @UserOperation(value = "查看角色", modelName = UserOperationModuleEnum.PERMISSION)
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ElectromagneticResult<?> getRoles(@RequestBody RolePageDTO rolePageDTO) {
        return ElectromagneticResultUtil.success(roleService.getRoles(rolePageDTO));
    }

    @RequiredRole(AdminTypeEnum.SECURITY)
    @UserOperation(value = "查看某一角色的权限", modelName = UserOperationModuleEnum.PERMISSION)
    @GetMapping(value = "/getRoleByName")
    public ElectromagneticResult<?> getRoleByName(@RequestParam("roleName") String roleName) {
        return ElectromagneticResultUtil.success(roleService.getRoleByName(roleName));
    }

    @RequiredRole(AdminTypeEnum.SECURITY)
    @UserOperation(value = "获取角色名列表", modelName = UserOperationModuleEnum.PERMISSION)
    @GetMapping(value = "/getRoleNames")
    public ElectromagneticResult<?> getRoleNames() {
        return ElectromagneticResultUtil.success(roleService.getAllRoleNames());
    }

    @RequiredRole(AdminTypeEnum.SECURITY)
    @UserOperation(value = "获取角色配置模版", modelName = UserOperationModuleEnum.PERMISSION)
    @GetMapping(value = "/getRoleTemplate")
    public ElectromagneticResult<?> getRoleTemplate() {
        return ElectromagneticResultUtil.success(roleService.getRoleTemplate());
    }
}
